//
//  HBVisitorView.swift
//  sinaWeibo
//
//  Created by Beans on 16/6/23.
//  Copyright © 2016年 iceWorks. All rights reserved.
//

/*
添加控件
布局
实现功能
*/

import UIKit
import SnapKit

// 定义宏的时候, 可以先在本类里定义, 实现功能以后, 再放到Constant
private let Margin = 10

// 代理  第一步
// 继承自 NSObjectProtocol, 因为delegate需要用weak
// 协议还可继承自 AnyObject
protocol HBVisitorViewDelegate: NSObjectProtocol {
    func didLogin()
    func didRegister()
}

class HBVisitorView: UIView {

    // 第二步 声明代理属性
    var delegate: HBVisitorViewDelegate?

    /** 自定义视图三部曲 */

    // 一. 重写构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // 二. 定义一个方法, 来实现视图的添加和布局
    private func setupUI() {

        // 设置背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)

        addSubview(trunImageView)
        addSubview(maskImageView)
        addSubview(houseImageView)  // 注意添加顺序. 当然也可以在trun上添加mask, mask上添加house

        addSubview(noticeLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)


        // 布局
        houseImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }

        trunImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }

        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }

        noticeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(trunImageView.snp_bottom).offset(Margin)
            make.width.equalTo(216)
        }

        registerBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(noticeLabel.snp_left)
            make.top.equalTo(noticeLabel.snp_bottom).offset(Margin)
            // W & H
            make.width.equalTo(100)
            make.height.equalTo(30)
        }

        loginBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(noticeLabel.snp_right)
            make.top.equalTo(noticeLabel.snp_bottom).offset(Margin)

            make.width.equalTo(100)
            make.height.equalTo(30)
        }

    }

    // MARK: - 动画的实现
    func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 10
        // 防止切换视图时动画停止
        animation.removedOnCompletion = false
        trunImageView.layer.addAnimation(animation, forKey: nil)
    }

    // MARK: - 对外公开方法, 供控制器修改属性
    func setupInfo(isTrun: Bool, image: String, message: String) {

        // 1. 让转动隐藏
        if isTrun {
            trunImageView.hidden = false
            startAnimation()
        } else {
            trunImageView.hidden = true
        }

        // 2. 改变中间图标
        houseImageView.image = UIImage(named: image)

        // 3. 改变提示语
        noticeLabel.text = message
    }

    // MARK: - 按钮的点击事件
    @objc private func login(btn: UIButton) {
        // 代理第三部 调用
        // OC中需要判断一下有没有实现; swift中不需判断, 因为delegate是可选项
        delegate?.didLogin()
    }

    @objc private func register(btn: UIButton) {
        delegate?.didRegister()
    }

    // 三. 懒加载控件
    // 首页小房子
    private lazy var houseImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        return img
    }()

    // 首页转动图片
    private lazy var trunImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return img
    }()

    // 蒙版, 遮盖动画下半部
    private lazy var maskImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return img
    }()

    private lazy var noticeLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15.0)
        label.numberOfLines = 0
        label.text = "关注一些人，回这里看看有什么惊喜. 看看看看👀👀(⊙o⊙)"
        label.textAlignment = NSTextAlignment.Center
        return label
    }()

    private lazy var registerBtn: UIButton = HBFactory.getButton("注册", titleColor: .orangeColor(), font: 18.0, image: "", bgImage: "common_button_white_disable", tag: 0, target: self, method: "register:")

    private lazy var loginBtn: UIButton = {
        let btn = UIButton(type: .Custom)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.addTarget(self, action: "login:", forControlEvents: .TouchUpInside)
        return btn
    }()

}
